<?php

	include_once("cmsConfig.php");


$cmsDocument = new CmsDocument($db);
$binaryData = new CmsTableWriter("binarydata", $db);
$folderDocumentCode = extractNumberWithKeyFromArray("folderDocumentCode", $_GET,null);


$cmsDocument->flush();
$cmsDocument->addDocumentType('docFileMod');//docFileMod // 220

if (is_null($folderDocumentCode)){
	$cmsDocument->addConstraint('categorycode', '<<NULL>>', 'IS');
} else {
	$cmsDocument->addConstraint('categorycode', $folderDocumentCode);
}

$cmsDocument->setFetchKeys(
	array(
		"documentcode" => "id",
		"content" => "file",
		"author" => "isFolder",
		"title" => "name",
		"categorycode" => "parentFolder"
	)
);

		
$cmsDocument->resetIterator();
//$fileItems = $cmsDocument->getAllDocuments();

$folders = array();
while ($fileItem = $cmsDocument->nextDocument()) {
	//look up the binary data for the image2wbmp
	if ( !$fileItem->isFolder){
	
		$binaryRecord = getBinaryRecordForDocumentCodeAndId($fileItem->id, $fileItem->file, $db);
		$fileItem->isImage = isMimeTypeImage($binaryRecord['mimetype']);
		$fileItem->isFLV = isMimeTypeFLV($binaryRecord['mimetype']);
		$fileItem->isVideo = isMimeTypeVideo($binaryRecord['mimetype']);
		$fileItem->isPDF = isMimeTypePDF($binaryRecord['mimetype']);
		$fileItem->isOtherFile = !($fileItem->isImage || $fileItem->isFLV || $fileItem->isVideo || $fileItem->isPDF);
		$fileItem->hasConvertedFLV = false;
		
		$fileItem->isFolder = $fileItem->isFolder ? true : false;//ensure boolean
		$fileItem->id = $fileItem->id * 1;//ensure a number
		
		// we have a non flv video.. try and find the flv representation
		if ($fileItem->isVideo && !$fileItem->isFLV) { }
	}
	array_push($folders,$fileItem);
}

header("content-type: application/json", true);

if (!$folders)  {
	echo json_encode(array());
} else {
	echo json_encode($folders);
}